package sgi.client;
import sgi.details_new.ComplaintDetails;

import java.io.IOException;
import java.util.Iterator;

import sgi.message.MessageGetComplaint;
import sgi.message.MessageCompensation;
import sgi.message.MessageDeclineComplaint;
public class SGIComplaintReviewController extends SGIController {

	public static SGIComplaintReviewController complaintReview;
	public static SGIComplaintReviewUI complaintReviewUI;
	
	/** creating the controller */
	public static SGIComplaintReviewController create() throws IOException
	{
		if(SGIComplaintReviewController.complaintReview == null)
		{
			SGIComplaintReviewController.complaintReview = new SGIComplaintReviewController();
			complaintReviewUI.setComplaintReviewController(SGIComplaintReviewController.complaintReview);
		}
		SGIComplaintReviewController.complaintReviewUI.setVisible(true);
		return complaintReview;
	}
	/** sending message to get the complaint list */
	public void ReviewComplaint()
	{
		MessageGetComplaint msg = new MessageGetComplaint();
		sendComplaintReview(msg);
	}
	/** sending message */
	private void sendComplaintReview(MessageGetComplaint msg)
	{
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** terminating UI */
	public static void terminate()
	{
		SGIComplaintReviewController.complaintReviewUI.setVisible(false);
		SGIComplaintReviewController.complaintReview=null;
		SGIComplaintReviewController.complaintReviewUI.setVisible(false);
		SGIComplaintReviewController.complaintReviewUI=null;
	}
	/** constructor */
	private SGIComplaintReviewController() 
	{
		SGIComplaintReviewController.complaintReviewUI = new SGIComplaintReviewUI();
	}
	
	/** sending message of accept complaint */
	public void handleMessageFromComplaintReviewUI(String userName,int balance, String dateTime) throws IOException
	{
		MessageCompensation msg = new MessageCompensation(userName,balance,dateTime);
		try {
				SGIClientConnectionController conn = SGIClientConnectionController.createConnection();
				conn.handleMessageFromClientUI(msg);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	/** sending message of decline complaint */
	public void handleDeclineMessageFromComplaintReviewUI(String userName,String dateTime) throws IOException
	{
		MessageDeclineComplaint msg= new MessageDeclineComplaint(userName,dateTime);
		try {
				SGIClientConnectionController conn = SGIClientConnectionController.createConnection();
				conn.handleMessageFromClientUI(msg);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	/** handle message to add complaints to table */
	@Override
	void handleMessage(Object msg)
	{
		Iterator<ComplaintDetails> iterator = (((MessageGetComplaint)msg).getComplaints()).iterator();
		SGIComplaintReviewController.complaintReviewUI.clearTable();
		while (iterator.hasNext()) {
			SGIComplaintReviewController.complaintReviewUI.addRows(iterator.next());
		}
	}
	/** getting the UI of complaint review */
	public static SGIComplaintReviewUI getUI()
	{
		return complaintReviewUI;
	}
}

